package com.hlj.smartpad.launcher;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.FileUtils;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Bitmap 帮助类之一
 */
class BitmapUtils {

    static String saveBitmap(String name, Bitmap bm, Context mContext) {
        String filePath = "";
        String TargetPath = mContext.getFilesDir() + "/images/";
        if (!fileIsExist(TargetPath)) {
            Log.d("Save Bitmap", "TargetPath isn't exist");
        } else {
            File saveFile = new File(TargetPath, name + ".jpg");
            filePath = saveFile.getPath();
            try {
                FileOutputStream saveImgOut = new FileOutputStream(saveFile);
                bm.compress(Bitmap.CompressFormat.JPEG, 80, saveImgOut);
                saveImgOut.flush();
                saveImgOut.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return filePath;
    }

    static boolean fileIsExist(String fileName) {
        File file = new File(fileName);
        if (file.exists())
            return true;
        else {
            return file.mkdirs();
        }
    }
}